﻿using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using LibKetoan;
using System.Reflection;
using System.Runtime.InteropServices;
using Excel;

namespace Ketoan
{
	/// <summary>
	/// Summary description for frmDm.
	/// </summary>
	public class frmDmtk : System.Windows.Forms.Form
	{
		private System.Windows.Forms.DataGrid dataGrid1;
		private System.Windows.Forms.Button butKetthuc;
		private AccessData a;
		private System.Data.DataTable dt=new System.Data.DataTable();
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.Button butMoi;
		private System.Windows.Forms.Button butSua;
		private System.Windows.Forms.Button butLuu;
		private System.Windows.Forms.Button butBoqua;
		private System.Windows.Forms.Button butHuy;
		private long l_id;		
		private int i_userid;
		private string s_mmyy,sql;
		private System.Windows.Forms.TextBox ten;
		private System.Windows.Forms.TextBox ma;
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.Label label4;
		private System.Windows.Forms.TextBox tim;
		Excel.Application oxl;
		Excel._Workbook owb;
		Excel._Worksheet osheet;
		private System.Windows.Forms.ComboBox tonghop;
		private System.Windows.Forms.ComboBox tinhchat;
		private System.Windows.Forms.Label label3;
		private System.Windows.Forms.Label label10;
		private System.Windows.Forms.CheckBox congno;
		private System.Windows.Forms.CheckBox kiemtrasd;
		private System.Windows.Forms.CheckBox mlns;
		private System.Windows.Forms.CheckBox ctmlns;
		private System.Windows.Forms.CheckBox bcdtk;
		private System.Windows.Forms.CheckBox sudung;
		private System.Windows.Forms.ComboBox loaikm;
		private System.Windows.Forms.CheckBox loaikhoan;
		private System.Windows.Forms.CheckBox nghiepvu;
		private System.Windows.Forms.CheckBox chuong;
		private System.Windows.Forms.Label label5;
		private System.Windows.Forms.ComboBox nhom;
		private System.Windows.Forms.CheckBox nhombc;
        private System.Windows.Forms.CheckBox khoanmuc;
        private System.Windows.Forms.CheckBox hoatdong;
		private System.Windows.Forms.Button butExcel;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public frmDmtk(AccessData acc,string mmyy,int userid)
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();
			a=acc;s_mmyy=mmyy;i_userid=userid;
			//
			// TODO: Add any constructor code after InitializeComponent call
			//
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(frmDmtk));
            this.dataGrid1 = new System.Windows.Forms.DataGrid();
            this.butKetthuc = new System.Windows.Forms.Button();
            this.label2 = new System.Windows.Forms.Label();
            this.ten = new System.Windows.Forms.TextBox();
            this.butMoi = new System.Windows.Forms.Button();
            this.butSua = new System.Windows.Forms.Button();
            this.butLuu = new System.Windows.Forms.Button();
            this.butBoqua = new System.Windows.Forms.Button();
            this.butHuy = new System.Windows.Forms.Button();
            this.ma = new System.Windows.Forms.TextBox();
            this.label1 = new System.Windows.Forms.Label();
            this.label4 = new System.Windows.Forms.Label();
            this.tonghop = new System.Windows.Forms.ComboBox();
            this.tim = new System.Windows.Forms.TextBox();
            this.sudung = new System.Windows.Forms.CheckBox();
            this.tinhchat = new System.Windows.Forms.ComboBox();
            this.label3 = new System.Windows.Forms.Label();
            this.label10 = new System.Windows.Forms.Label();
            this.loaikm = new System.Windows.Forms.ComboBox();
            this.congno = new System.Windows.Forms.CheckBox();
            this.kiemtrasd = new System.Windows.Forms.CheckBox();
            this.mlns = new System.Windows.Forms.CheckBox();
            this.ctmlns = new System.Windows.Forms.CheckBox();
            this.bcdtk = new System.Windows.Forms.CheckBox();
            this.loaikhoan = new System.Windows.Forms.CheckBox();
            this.nghiepvu = new System.Windows.Forms.CheckBox();
            this.chuong = new System.Windows.Forms.CheckBox();
            this.label5 = new System.Windows.Forms.Label();
            this.nhom = new System.Windows.Forms.ComboBox();
            this.nhombc = new System.Windows.Forms.CheckBox();
            this.khoanmuc = new System.Windows.Forms.CheckBox();
            this.hoatdong = new System.Windows.Forms.CheckBox();
            this.butExcel = new System.Windows.Forms.Button();
            ((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();
            this.SuspendLayout();
            // 
            // dataGrid1
            // 
            this.dataGrid1.AlternatingBackColor = System.Drawing.Color.Lavender;
            this.dataGrid1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
                        | System.Windows.Forms.AnchorStyles.Left)
                        | System.Windows.Forms.AnchorStyles.Right)));
            this.dataGrid1.BackColor = System.Drawing.Color.WhiteSmoke;
            this.dataGrid1.BackgroundColor = System.Drawing.SystemColors.Control;
            this.dataGrid1.BorderStyle = System.Windows.Forms.BorderStyle.None;
            this.dataGrid1.CaptionBackColor = System.Drawing.SystemColors.Control;
            this.dataGrid1.CaptionFont = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.dataGrid1.CaptionForeColor = System.Drawing.Color.MidnightBlue;
            this.dataGrid1.DataMember = "";
            this.dataGrid1.FlatMode = true;
            this.dataGrid1.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.dataGrid1.ForeColor = System.Drawing.Color.MidnightBlue;
            this.dataGrid1.GridLineColor = System.Drawing.Color.Gainsboro;
            this.dataGrid1.GridLineStyle = System.Windows.Forms.DataGridLineStyle.None;
            this.dataGrid1.HeaderFont = new System.Drawing.Font("Tahoma", 8F, System.Drawing.FontStyle.Bold);
            this.dataGrid1.HeaderForeColor = System.Drawing.Color.WhiteSmoke;
            this.dataGrid1.LinkColor = System.Drawing.Color.Teal;
            this.dataGrid1.Location = new System.Drawing.Point(8, 9);
            this.dataGrid1.Name = "dataGrid1";
            this.dataGrid1.ParentRowsBackColor = System.Drawing.Color.Gainsboro;
            this.dataGrid1.ParentRowsForeColor = System.Drawing.Color.MidnightBlue;
            this.dataGrid1.ReadOnly = true;
            this.dataGrid1.RowHeaderWidth = 10;
            this.dataGrid1.SelectionBackColor = System.Drawing.Color.CadetBlue;
            this.dataGrid1.SelectionForeColor = System.Drawing.Color.WhiteSmoke;
            this.dataGrid1.Size = new System.Drawing.Size(846, 391);
            this.dataGrid1.TabIndex = 41;
            this.dataGrid1.CurrentCellChanged += new System.EventHandler(this.dataGrid1_CurrentCellChanged);
            // 
            // butKetthuc
            // 
            this.butKetthuc.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
            this.butKetthuc.Image = ((System.Drawing.Image)(resources.GetObject("butKetthuc.Image")));
            this.butKetthuc.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
            this.butKetthuc.Location = new System.Drawing.Point(547, 504);
            this.butKetthuc.Name = "butKetthuc";
            this.butKetthuc.Size = new System.Drawing.Size(70, 25);
            this.butKetthuc.TabIndex = 39;
            this.butKetthuc.Text = "&Kết thúc";
            this.butKetthuc.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            this.butKetthuc.Click += new System.EventHandler(this.butKetthuc_Click);
            // 
            // label2
            // 
            this.label2.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
            this.label2.Location = new System.Drawing.Point(200, 406);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(88, 23);
            this.label2.TabIndex = 2;
            this.label2.Text = "Tên tài khoản :";
            this.label2.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            // 
            // ten
            // 
            this.ten.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)
                        | System.Windows.Forms.AnchorStyles.Right)));
            this.ten.BackColor = System.Drawing.SystemColors.HighlightText;
            this.ten.Enabled = false;
            this.ten.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.ten.Location = new System.Drawing.Point(288, 406);
            this.ten.Name = "ten";
            this.ten.Size = new System.Drawing.Size(358, 21);
            this.ten.TabIndex = 3;
            this.ten.Validated += new System.EventHandler(this.ten_Validated);
            this.ten.KeyDown += new System.Windows.Forms.KeyEventHandler(this.ten_KeyDown);
            // 
            // butMoi
            // 
            this.butMoi.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
            this.butMoi.Image = ((System.Drawing.Image)(resources.GetObject("butMoi.Image")));
            this.butMoi.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
            this.butMoi.Location = new System.Drawing.Point(175, 504);
            this.butMoi.Name = "butMoi";
            this.butMoi.Size = new System.Drawing.Size(60, 25);
            this.butMoi.TabIndex = 35;
            this.butMoi.Text = "     &Mới";
            this.butMoi.Click += new System.EventHandler(this.butMoi_Click);
            // 
            // butSua
            // 
            this.butSua.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
            this.butSua.Image = ((System.Drawing.Image)(resources.GetObject("butSua.Image")));
            this.butSua.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
            this.butSua.Location = new System.Drawing.Point(237, 504);
            this.butSua.Name = "butSua";
            this.butSua.Size = new System.Drawing.Size(60, 25);
            this.butSua.TabIndex = 36;
            this.butSua.Text = "    &Sửa";
            this.butSua.Click += new System.EventHandler(this.butSua_Click);
            // 
            // butLuu
            // 
            this.butLuu.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
            this.butLuu.Enabled = false;
            this.butLuu.Image = ((System.Drawing.Image)(resources.GetObject("butLuu.Image")));
            this.butLuu.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
            this.butLuu.Location = new System.Drawing.Point(299, 504);
            this.butLuu.Name = "butLuu";
            this.butLuu.Size = new System.Drawing.Size(60, 25);
            this.butLuu.TabIndex = 33;
            this.butLuu.Text = "     &Lưu";
            this.butLuu.Click += new System.EventHandler(this.butLuu_Click);
            // 
            // butBoqua
            // 
            this.butBoqua.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
            this.butBoqua.Enabled = false;
            this.butBoqua.Image = ((System.Drawing.Image)(resources.GetObject("butBoqua.Image")));
            this.butBoqua.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
            this.butBoqua.Location = new System.Drawing.Point(361, 504);
            this.butBoqua.Name = "butBoqua";
            this.butBoqua.Size = new System.Drawing.Size(60, 25);
            this.butBoqua.TabIndex = 34;
            this.butBoqua.Text = "&Bỏ qua";
            this.butBoqua.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            this.butBoqua.Click += new System.EventHandler(this.butBoqua_Click);
            // 
            // butHuy
            // 
            this.butHuy.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
            this.butHuy.Image = ((System.Drawing.Image)(resources.GetObject("butHuy.Image")));
            this.butHuy.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
            this.butHuy.Location = new System.Drawing.Point(485, 504);
            this.butHuy.Name = "butHuy";
            this.butHuy.Size = new System.Drawing.Size(60, 25);
            this.butHuy.TabIndex = 38;
            this.butHuy.Text = "    &Hủy";
            this.butHuy.Click += new System.EventHandler(this.butHuy_Click);
            // 
            // ma
            // 
            this.ma.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
            this.ma.BackColor = System.Drawing.SystemColors.HighlightText;
            this.ma.CharacterCasing = System.Windows.Forms.CharacterCasing.Upper;
            this.ma.Enabled = false;
            this.ma.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.ma.Location = new System.Drawing.Point(56, 406);
            this.ma.MaxLength = 20;
            this.ma.Name = "ma";
            this.ma.Size = new System.Drawing.Size(152, 21);
            this.ma.TabIndex = 1;
            this.ma.Validated += new System.EventHandler(this.ma_Validated);
            this.ma.KeyDown += new System.Windows.Forms.KeyEventHandler(this.ma_KeyDown);
            // 
            // label1
            // 
            this.label1.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
            this.label1.Location = new System.Drawing.Point(11, 406);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(48, 23);
            this.label1.TabIndex = 0;
            this.label1.Text = "Số TK :";
            this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            // 
            // label4
            // 
            this.label4.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
            this.label4.Location = new System.Drawing.Point(638, 406);
            this.label4.Name = "label4";
            this.label4.Size = new System.Drawing.Size(88, 23);
            this.label4.TabIndex = 4;
            this.label4.Text = "Tài khoản mẹ :";
            this.label4.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            this.label4.Click += new System.EventHandler(this.label4_Click);
            // 
            // tonghop
            // 
            this.tonghop.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
            this.tonghop.Enabled = false;
            this.tonghop.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.tonghop.Location = new System.Drawing.Point(726, 406);
            this.tonghop.Name = "tonghop";
            this.tonghop.Size = new System.Drawing.Size(128, 21);
            this.tonghop.TabIndex = 5;
            this.tonghop.KeyDown += new System.Windows.Forms.KeyEventHandler(this.ma_KeyDown);
            // 
            // tim
            // 
            this.tim.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
                        | System.Windows.Forms.AnchorStyles.Right)));
            this.tim.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.tim.Location = new System.Drawing.Point(8, 5);
            this.tim.Name = "tim";
            this.tim.Size = new System.Drawing.Size(846, 21);
            this.tim.TabIndex = 40;
            this.tim.Text = "Tìm kiếm";
            this.tim.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
            this.tim.TextChanged += new System.EventHandler(this.tim_TextChanged);
            this.tim.Enter += new System.EventHandler(this.tim_Enter);
            // 
            // sudung
            // 
            this.sudung.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
            this.sudung.Enabled = false;
            this.sudung.Location = new System.Drawing.Point(742, 481);
            this.sudung.Name = "sudung";
            this.sudung.Size = new System.Drawing.Size(72, 16);
            this.sudung.TabIndex = 32;
            this.sudung.Text = "Sử dụng";
            this.sudung.KeyDown += new System.Windows.Forms.KeyEventHandler(this.stt_KeyDown);
            // 
            // tinhchat
            // 
            this.tinhchat.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)
                        | System.Windows.Forms.AnchorStyles.Right)));
            this.tinhchat.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
            this.tinhchat.Enabled = false;
            this.tinhchat.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.tinhchat.Location = new System.Drawing.Point(288, 429);
            this.tinhchat.Name = "tinhchat";
            this.tinhchat.Size = new System.Drawing.Size(566, 21);
            this.tinhchat.TabIndex = 9;
            this.tinhchat.KeyDown += new System.Windows.Forms.KeyEventHandler(this.ma_KeyDown);
            // 
            // label3
            // 
            this.label3.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
            this.label3.Location = new System.Drawing.Point(224, 429);
            this.label3.Name = "label3";
            this.label3.Size = new System.Drawing.Size(64, 23);
            this.label3.TabIndex = 8;
            this.label3.Text = "Tính chất :";
            this.label3.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            // 
            // label10
            // 
            this.label10.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)
                        | System.Windows.Forms.AnchorStyles.Right)));
            this.label10.Location = new System.Drawing.Point(581, 429);
            this.label10.Name = "label10";
            this.label10.Size = new System.Drawing.Size(0, 0);
            this.label10.TabIndex = 10;
            this.label10.Text = "Nguồn :";
            this.label10.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            // 
            // loaikm
            // 
            this.loaikm.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
            this.loaikm.Enabled = false;
            this.loaikm.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.loaikm.Location = new System.Drawing.Point(726, 429);
            this.loaikm.Name = "loaikm";
            this.loaikm.Size = new System.Drawing.Size(0, 21);
            this.loaikm.TabIndex = 11;
            this.loaikm.KeyDown += new System.Windows.Forms.KeyEventHandler(this.ma_KeyDown);
            // 
            // congno
            // 
            this.congno.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
            this.congno.Enabled = false;
            this.congno.Location = new System.Drawing.Point(319, 462);
            this.congno.Name = "congno";
            this.congno.Size = new System.Drawing.Size(72, 16);
            this.congno.TabIndex = 20;
            this.congno.Text = "Công nợ";
            this.congno.KeyDown += new System.Windows.Forms.KeyEventHandler(this.ma_KeyDown);
            // 
            // kiemtrasd
            // 
            this.kiemtrasd.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
            this.kiemtrasd.Enabled = false;
            this.kiemtrasd.Location = new System.Drawing.Point(216, 462);
            this.kiemtrasd.Name = "kiemtrasd";
            this.kiemtrasd.Size = new System.Drawing.Size(96, 16);
            this.kiemtrasd.TabIndex = 19;
            this.kiemtrasd.Text = "Kiểm tra số dư";
            this.kiemtrasd.KeyDown += new System.Windows.Forms.KeyEventHandler(this.ma_KeyDown);
            // 
            // mlns
            // 
            this.mlns.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
            this.mlns.Enabled = false;
            this.mlns.Location = new System.Drawing.Point(441, 462);
            this.mlns.Name = "mlns";
            this.mlns.Size = new System.Drawing.Size(120, 16);
            this.mlns.TabIndex = 21;
            this.mlns.Text = "Mục lục ngân sách";
            this.mlns.CheckedChanged += new System.EventHandler(this.mlns_CheckedChanged);
            this.mlns.KeyDown += new System.Windows.Forms.KeyEventHandler(this.ma_KeyDown);
            // 
            // ctmlns
            // 
            this.ctmlns.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
            this.ctmlns.Enabled = false;
            this.ctmlns.Location = new System.Drawing.Point(600, 462);
            this.ctmlns.Name = "ctmlns";
            this.ctmlns.Size = new System.Drawing.Size(96, 16);
            this.ctmlns.TabIndex = 22;
            this.ctmlns.Text = "Chi tiết MLNS";
            this.ctmlns.KeyDown += new System.Windows.Forms.KeyEventHandler(this.ma_KeyDown);
            // 
            // bcdtk
            // 
            this.bcdtk.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
            this.bcdtk.Enabled = false;
            this.bcdtk.Location = new System.Drawing.Point(72, 461);
            this.bcdtk.Name = "bcdtk";
            this.bcdtk.Size = new System.Drawing.Size(144, 16);
            this.bcdtk.TabIndex = 18;
            this.bcdtk.Text = "Bảng cân đối tài khoản";
            this.bcdtk.KeyDown += new System.Windows.Forms.KeyEventHandler(this.ma_KeyDown);
            // 
            // loaikhoan
            // 
            this.loaikhoan.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
            this.loaikhoan.Enabled = false;
            this.loaikhoan.Location = new System.Drawing.Point(72, 481);
            this.loaikhoan.Name = "loaikhoan";
            this.loaikhoan.Size = new System.Drawing.Size(80, 16);
            this.loaikhoan.TabIndex = 24;
            this.loaikhoan.Text = "Loại khoản";
            this.loaikhoan.KeyDown += new System.Windows.Forms.KeyEventHandler(this.ma_KeyDown);
            // 
            // nghiepvu
            // 
            this.nghiepvu.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
            this.nghiepvu.Enabled = false;
            this.nghiepvu.Location = new System.Drawing.Point(742, 461);
            this.nghiepvu.Name = "nghiepvu";
            this.nghiepvu.Size = new System.Drawing.Size(80, 16);
            this.nghiepvu.TabIndex = 23;
            this.nghiepvu.Text = "Nghiệp vụ";
            this.nghiepvu.KeyDown += new System.Windows.Forms.KeyEventHandler(this.ma_KeyDown);
            // 
            // chuong
            // 
            this.chuong.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
            this.chuong.Enabled = false;
            this.chuong.Location = new System.Drawing.Point(318, 481);
            this.chuong.Name = "chuong";
            this.chuong.Size = new System.Drawing.Size(64, 16);
            this.chuong.TabIndex = 26;
            this.chuong.Text = "Chương";
            this.chuong.KeyDown += new System.Windows.Forms.KeyEventHandler(this.ma_KeyDown);
            // 
            // label5
            // 
            this.label5.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
            this.label5.Location = new System.Drawing.Point(-5, 429);
            this.label5.Name = "label5";
            this.label5.Size = new System.Drawing.Size(64, 23);
            this.label5.TabIndex = 6;
            this.label5.Text = "Nhóm :";
            this.label5.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            // 
            // nhom
            // 
            this.nhom.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
            this.nhom.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
            this.nhom.Enabled = false;
            this.nhom.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.nhom.Location = new System.Drawing.Point(56, 429);
            this.nhom.Name = "nhom";
            this.nhom.Size = new System.Drawing.Size(152, 21);
            this.nhom.TabIndex = 7;
            this.nhom.KeyDown += new System.Windows.Forms.KeyEventHandler(this.ma_KeyDown);
            // 
            // nhombc
            // 
            this.nhombc.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
            this.nhombc.Enabled = false;
            this.nhombc.Location = new System.Drawing.Point(441, 481);
            this.nhombc.Name = "nhombc";
            this.nhombc.Size = new System.Drawing.Size(104, 16);
            this.nhombc.TabIndex = 27;
            this.nhombc.Text = "Nhóm báo cáo";
            this.nhombc.KeyDown += new System.Windows.Forms.KeyEventHandler(this.ma_KeyDown);
            // 
            // khoanmuc
            // 
            this.khoanmuc.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
            this.khoanmuc.Enabled = false;
            this.khoanmuc.Location = new System.Drawing.Point(600, 481);
            this.khoanmuc.Name = "khoanmuc";
            this.khoanmuc.Size = new System.Drawing.Size(0, 0);
            this.khoanmuc.TabIndex = 28;
            this.khoanmuc.Text = "Nguồn";
            this.khoanmuc.Visible = false;
            this.khoanmuc.KeyDown += new System.Windows.Forms.KeyEventHandler(this.ma_KeyDown);
            // 
            // hoatdong
            // 
            this.hoatdong.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
            this.hoatdong.Enabled = false;
            this.hoatdong.Location = new System.Drawing.Point(216, 481);
            this.hoatdong.Name = "hoatdong";
            this.hoatdong.Size = new System.Drawing.Size(80, 16);
            this.hoatdong.TabIndex = 25;
            this.hoatdong.Text = "Hoạt động";
            this.hoatdong.KeyDown += new System.Windows.Forms.KeyEventHandler(this.ma_KeyDown);
            // 
            // butExcel
            // 
            this.butExcel.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
            this.butExcel.Image = ((System.Drawing.Image)(resources.GetObject("butExcel.Image")));
            this.butExcel.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
            this.butExcel.Location = new System.Drawing.Point(424, 504);
            this.butExcel.Name = "butExcel";
            this.butExcel.Size = new System.Drawing.Size(60, 25);
            this.butExcel.TabIndex = 88;
            this.butExcel.Text = "      &In";
            this.butExcel.Click += new System.EventHandler(this.butExcel_Click);
            // 
            // frmDmtk
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.ClientSize = new System.Drawing.Size(862, 573);
            this.Controls.Add(this.butExcel);
            this.Controls.Add(this.sudung);
            this.Controls.Add(this.khoanmuc);
            this.Controls.Add(this.nhombc);
            this.Controls.Add(this.tinhchat);
            this.Controls.Add(this.nhom);
            this.Controls.Add(this.label5);
            this.Controls.Add(this.chuong);
            this.Controls.Add(this.hoatdong);
            this.Controls.Add(this.loaikhoan);
            this.Controls.Add(this.nghiepvu);
            this.Controls.Add(this.bcdtk);
            this.Controls.Add(this.ctmlns);
            this.Controls.Add(this.mlns);
            this.Controls.Add(this.kiemtrasd);
            this.Controls.Add(this.congno);
            this.Controls.Add(this.label10);
            this.Controls.Add(this.loaikm);
            this.Controls.Add(this.label3);
            this.Controls.Add(this.tim);
            this.Controls.Add(this.ten);
            this.Controls.Add(this.tonghop);
            this.Controls.Add(this.label4);
            this.Controls.Add(this.ma);
            this.Controls.Add(this.label1);
            this.Controls.Add(this.butHuy);
            this.Controls.Add(this.butBoqua);
            this.Controls.Add(this.butLuu);
            this.Controls.Add(this.butSua);
            this.Controls.Add(this.butMoi);
            this.Controls.Add(this.label2);
            this.Controls.Add(this.butKetthuc);
            this.Controls.Add(this.dataGrid1);
            this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
            this.Name = "frmDmtk";
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
            this.Text = "Danh mục tài khoản";
            this.Load += new System.EventHandler(this.frmDmtk_Load);
            this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.frmDmtk_KeyDown);
            ((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
            this.ResumeLayout(false);
            this.PerformLayout();

		}
		#endregion

		private void frmDmtk_Load(object sender, System.EventArgs e)
		{
			tonghop.ValueMember="STK";
			tonghop.DisplayMember="STK";

			loaikm.DisplayMember="TEN";
			loaikm.ValueMember="ID";
			loaikm.DataSource=a.get_data("select * from a_dmloaikm where sudung=1 order by stt").Tables[0];


			tinhchat.DisplayMember="TEN";
			tinhchat.ValueMember="ID";
			tinhchat.DataSource=a.get_data("select * from a_dmtctk order by stt").Tables[0];

			nhom.DisplayMember="TEN";
			nhom.ValueMember="ID";
			nhom.DataSource=a.get_data("select * from a_dmnhomtk where sudung=1 order by stt").Tables[0];

			load_grid();
			AddGridTableStyle();
			ref_text();
		}

		private void load_grid()
		{
			dt=a.get_data("select a.*,b.ten as tentinhchat,nvl(a.tonghop,' ') as thop from a_dmtk a,a_dmtctk b where a.tinhchat=b.id order by a.stk").Tables[0];
			dataGrid1.DataSource=dt;
		}

		private void AddGridTableStyle()
		{
			DataGridTableStyle ts =new DataGridTableStyle();
			ts.MappingName = dt.TableName;
			ts.AlternatingBackColor = Color.Beige;
			ts.BackColor = Color.GhostWhite;
			ts.ForeColor = Color.MidnightBlue;
			ts.GridLineColor = Color.RoyalBlue;
			ts.HeaderBackColor = Color.MidnightBlue;
			ts.HeaderForeColor = Color.Lavender;
			ts.SelectionBackColor = Color.Teal;
			ts.SelectionForeColor = Color.PaleGreen;
			ts.ReadOnly=false;
			ts.RowHeaderWidth=10;
						
			DataGridTextBoxColumn TextCol=new DataGridTextBoxColumn();
			TextCol.MappingName = "id";
			TextCol.HeaderText = "";
			TextCol.Width = 0;
			ts.GridColumnStyles.Add(TextCol);
			dataGrid1.TableStyles.Add(ts);

			TextCol=new DataGridTextBoxColumn();
			TextCol.MappingName = "stk";
			TextCol.HeaderText = "Số tài khoản";
			TextCol.Width = 120;
			ts.GridColumnStyles.Add(TextCol);
			dataGrid1.TableStyles.Add(ts);

			TextCol=new DataGridTextBoxColumn();
			TextCol.MappingName = "ttk";
			TextCol.HeaderText = "Tên tài khoản";
			TextCol.Width = dataGrid1.Width-100-80-80-35;
			ts.GridColumnStyles.Add(TextCol);
			dataGrid1.TableStyles.Add(ts);

			TextCol=new DataGridTextBoxColumn();
			TextCol.MappingName = "thop";
			TextCol.HeaderText = "Tài khoản gốc";
			TextCol.Width = 80;
			ts.GridColumnStyles.Add(TextCol);
			dataGrid1.TableStyles.Add(ts);

			TextCol=new DataGridTextBoxColumn();
			TextCol.MappingName = "tentinhchat";
			TextCol.HeaderText = "Tính chất";
			TextCol.Width = 60;
			ts.GridColumnStyles.Add(TextCol);
			dataGrid1.TableStyles.Add(ts);
		}

		private void butKetthuc_Click(object sender, System.EventArgs e)
		{
			this.Close();
		}

		private void ten_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
		{
			if (e.KeyCode==Keys.Enter) SendKeys.Send("{Tab}{F4}");
		}

		private void ena_object(bool ena)
		{
			dataGrid1.Enabled=!ena;
			ma.Enabled=ena;
			ten.Enabled=ena;
			tim.Enabled=!ena;
			tinhchat.Enabled=tonghop.Enabled=congno.Enabled=kiemtrasd.Enabled=mlns.Enabled=bcdtk.Enabled=loaikhoan.Enabled=ena;
            nhom.Enabled = loaikm.Enabled = nghiepvu.Enabled = hoatdong.Enabled = chuong.Enabled = sudung.Enabled = ena;
			nhombc.Enabled=khoanmuc.Enabled=ena;
			butMoi.Enabled=!ena;
			butSua.Enabled=!ena;
			butLuu.Enabled=ena;
			butBoqua.Enabled=ena;
			butExcel.Enabled=!ena;
			butHuy.Enabled=!ena;
			butKetthuc.Enabled=!ena;
		}

		private void butMoi_Click(object sender, System.EventArgs e)
		{
			l_id=0;
			ma.Text=ten.Text="";tonghop.SelectedIndex=-1;sudung.Checked=true;
			congno.Checked=kiemtrasd.Checked=mlns.Checked=ctmlns.Checked=bcdtk.Checked=nghiepvu.Checked=false;
			nhombc.Checked=khoanmuc.Checked=loaikhoan.Checked=hoatdong.Checked=chuong.Checked=false;
			ena_object(true);
			ma.Focus();
		}

		private void butSua_Click(object sender, System.EventArgs e)
		{
			if (dt.Rows.Count==0) return;
			l_id=long.Parse(dataGrid1[dataGrid1.CurrentCell.RowNumber,0].ToString());
			ena_object(true);
			ctmlns.Enabled=mlns.Checked;
			ma.Focus();
		}

		private bool kiemtra()
		{
			if (ma.Text=="")
			{
				ma.Focus();
				return false;
			}
			if (ten.Text=="")
			{
				ten.Focus();
				return false;
			}
			if (a.get_data("select * from "+a.user+".a_dmtk where stk='"+ma.Text+"' and id<>"+l_id).Tables[0].Rows.Count>0)
			{
				MessageBox.Show("Số tài khoản "+ma.Text+" đã nhập !",a.Msg);
				return false;
			}
			string id_tk="";
			id_tk=a.get_id_stk(tonghop.Text);
			string yy=DateTime.Now.Year.ToString().Substring(2,2);
			if(a.get_data("select tknoa,tkcoa from "+a.user+".a_ctct"+yy+" where tknoa='"+id_tk+"' or tkcoa='"+id_tk+"'" ).Tables[0].Rows.Count>0)
			{
				MessageBox.Show("Tài khoản "+tonghop.Text+" đang được sử dụng không được nhập tài khoản con",a.Msg);
				return false;
			}
			return true;
		}

		private int getSp()
		{
			int sp=0;
			if (tonghop.SelectedIndex!=-1)
			{
				sql="select sp from a_dmtk where stk='"+tonghop.SelectedValue.ToString()+"'";
				foreach(DataRow r in a.get_data(sql).Tables[0].Rows) sp=int.Parse(r["sp"].ToString());
			}
			return sp;
		}
		private void butLuu_Click(object sender, System.EventArgs e)
		{
			if (!kiemtra()) return ;
			l_id=(l_id==0)?a.getIddmtk():l_id;
			int tknb = 0, sp = 1;
			tknb = (ma.Text.Substring(0, 1) == "0") ? 1 : 0;
			if (tonghop.SelectedIndex!=-1) 
			{
				sp = getSp() + 1;
				a.execute_data("update a_dmtk set c=0 where stk='"+tonghop.SelectedValue.ToString()+"'");
			}
			if (!a.upd_dmtk(l_id,ma.Text.Trim(),ten.Text,tknb,sp,(congno.Checked)?1:0,int.Parse(tinhchat.SelectedValue.ToString()),(mlns.Checked)?1:0,(ctmlns.Checked)?1:0,(tonghop.SelectedIndex!=-1)?tonghop.SelectedValue.ToString():"",(bcdtk.Checked)?1:0,(kiemtrasd.Checked)?1:0,(sudung.Checked)?1:0,(loaikm.SelectedIndex!=-1)?int.Parse(loaikm.SelectedValue.ToString()):0,int.Parse(nhom.SelectedValue.ToString()),(nghiepvu.Checked)?1:0,(loaikhoan.Checked)?1:0,(hoatdong.Checked)?1:0,(chuong.Checked)?1:0,(nhombc.Checked)?1:0,(khoanmuc.Checked)?1:0,i_userid))
			{
				MessageBox.Show("Không cập nhật thông tin "+this.Text.Trim()+" !",a.Msg);
				return;
			}
			load_grid();
			ref_text();
			ena_object(false);
			ctmlns.Enabled=false;
		}

		private void butBoqua_Click(object sender, System.EventArgs e)
		{
			ref_text();
			ena_object(false);
			ctmlns.Enabled=false;
		}

		private void butHuy_Click(object sender, System.EventArgs e)
		{
			try
			{
				if (a.get_data("select * from a_sd"+s_mmyy.Substring(2,2)+" where stka="+long.Parse(dataGrid1[dataGrid1.CurrentCell.RowNumber,0].ToString())).Tables[0].Rows.Count!=0)
				{
					MessageBox.Show("Số tài khoản "+ma.Text+" đang sử dụng không cho phép hủy !",a.Msg);
					return;
				}
			}
			catch{}
			if (MessageBox.Show("Đồng ý hủy thông tin này !",a.Msg,MessageBoxButtons.YesNo,MessageBoxIcon.Question)==DialogResult.Yes)
			{
				string th=(tonghop.SelectedIndex!=-1)?tonghop.SelectedValue.ToString():"";
				a.execute_data("delete from a_dmtk where id="+long.Parse(dataGrid1[dataGrid1.CurrentCell.RowNumber,0].ToString()));
				if (th!="" && a.get_data("select * from a_dmtk where tonghop='"+th+"'").Tables[0].Rows.Count==0)
					a.execute_data("update a_dmtk set c=1 where stk='"+th+"'");
				load_grid();
			}
		}

		private void ref_text()
		{
			try
			{
				int i=dataGrid1.CurrentCell.RowNumber;
				l_id=long.Parse(dataGrid1[i,0].ToString());
				DataRow r=a.getrowbyid(dt,"id="+l_id);
				if (r!=null)
				{
					ma.Text=r["stk"].ToString();
					ten.Text=r["ttk"].ToString();
					tonghop.SelectedIndex=-1;
					load_tonghop();
					tonghop.SelectedValue=r["tonghop"].ToString();
					tinhchat.SelectedValue=r["tinhchat"].ToString();
					loaikm.SelectedValue=r["loaikm"].ToString();					
					nhom.SelectedValue=r["nhom"].ToString();
					congno.Checked=r["congno"].ToString()=="1";
					kiemtrasd.Checked=r["kiemtrasd"].ToString()=="1";
					mlns.Checked=r["mlns"].ToString()=="1";
					ctmlns.Checked=r["ctmlns"].ToString()=="1";
					bcdtk.Checked=r["bcdtk"].ToString()=="1";
					nghiepvu.Checked=r["nghiepvu"].ToString()=="1";
					hoatdong.Checked=r["hoatdong"].ToString()=="1";
					chuong.Checked=r["chuong"].ToString()=="1";
					loaikhoan.Checked=r["loaikhoan"].ToString()=="1";
					sudung.Checked=r["sudung"].ToString()=="1";
					nhombc.Checked=r["nhombc"].ToString()=="1";
					khoanmuc.Checked=r["khoanmuc"].ToString()=="1";					
				}
			}
			catch{}
		}

		private void load_tonghop()
		{
			sql = "select id,stk,ttk from a_dmtk where stk=substr('"+ma.Text+"',1,length(stk)) and stk<>'"+ma.Text+"' order by stk desc";
			tonghop.DataSource=a.get_data(sql).Tables[0];
		}

		private void dataGrid1_CurrentCellChanged(object sender, System.EventArgs e)
		{
			ref_text();
		}

		private void frmDmtk_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
		{
			if (e.KeyCode==Keys.F10) butKetthuc_Click(sender,e);
		}

		private void stt_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
		{
			if (e.KeyCode==Keys.Enter) SendKeys.Send("{Tab}");		
		}

		private void ma_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
		{
			if (e.KeyCode==Keys.Enter) SendKeys.Send("{Tab}");	
		}

		private void ma_Validated(object sender, System.EventArgs e)
		{
			if (ma.Text!="" && l_id==0)
			{
				DataRow r1=a.getrowbyid(dt,"ma='"+ma.Text+"'");
				if (r1!=null)
				{
					MessageBox.Show("Số tài khoản này đã nhập !",a.Msg);
					ma.Focus();
					return;
				}
				if (ten.Text=="") ten.Text=ma.Text;
				load_tonghop();
			}
		}

		private void ten_Validated(object sender, System.EventArgs e)
		{
			if (l_id==0 && ten.Text!="")
			{
				DataRow r1=a.getrowbyid(dt,"ten='"+ten.Text+"'");
				if (r1!=null)
				{
					MessageBox.Show("Tên tài khoản này đã nhập !",a.Msg);
					ten.Focus();
				}
			}
		}

		private void RefreshChildren(string text)
		{
			try
			{
				CurrencyManager cm= (CurrencyManager)BindingContext[dataGrid1.DataSource];
				DataView dv=(DataView)cm.List;
				dv.RowFilter="stk like '%"+text.Trim()+"%' or ttk like '%"+text.Trim()+"%'";
				ref_text();
			}
			catch{}
		}

		private void tim_TextChanged(object sender, System.EventArgs e)
		{
			if (this.ActiveControl==tim) RefreshChildren(tim.Text);
		}

		private void tim_Enter(object sender, System.EventArgs e)
		{
			tim.Text="";
		}

		private void butExcel_Click(object sender, System.EventArgs e)
		{

			DataSet _DataSet =new DataSet();
			_DataSet=a._rpthethongtaikhoan(tim.Text);
			frmReport _frmReport =new frmReport(a,_DataSet,"11","KT_danhmuchethongtaikhoan.rpt");
			_frmReport.ShowDialog();
//			a.check_process_Excel();
//			string tenfile=a.Export_Excel(dt,"dmtk");
//			oxl=new Excel.Application();
//			owb=(Excel._Workbook)(oxl.Workbooks.Open(tenfile,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value));
//			osheet=(Excel._Worksheet)owb.ActiveSheet;
//			oxl.ActiveWindow.DisplayGridlines=true;
//			oxl.ActiveWindow.DisplayZeros=false;
//			osheet.PageSetup.Orientation=XlPageOrientation.xlLandscape;
//			osheet.PageSetup.PaperSize=XlPaperSize.xlPaperA4;
//			osheet.PageSetup.LeftMargin=20;
//			osheet.PageSetup.RightMargin=20;
//			osheet.PageSetup.TopMargin=30;
//			osheet.PageSetup.CenterFooter="Trang : &P/&N";
//			osheet.get_Range("A2","A2").Select();
//			oxl.ActiveWindow.FreezePanes = true;
//			oxl.Visible=true;
		}

		private void mlns_CheckedChanged(object sender, System.EventArgs e)
		{
			if (this.ActiveControl==mlns) ctmlns.Enabled=mlns.Checked;
		}

        private void label4_Click(object sender, EventArgs e)
        {

        }

        private void label8_Click(object sender, EventArgs e)
        {

        }
	}
}
